package com.huatai.bpmn.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bpmn.entity.UserRoleEntity;
import com.huatai.bpmn.service.UserRoleService;
import com.huatai.bpmn.mapper.UserRoleMapper;
import com.huatai.bpmn.service.UserService;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import com.huatai.common.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author 聂贞吉
* @description 针对表【tb_user_role】的数据库操作Service实现
* @createDate 2023-07-13 14:27:58
*/
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleEntity> implements UserRoleService{

	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;

	@Override
	public UserDetails loadUserByUsername(String userId) {
		LambdaQueryWrapper<UserRoleEntity> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UserRoleEntity::getUserId,userId);
		List<UserRoleEntity> userRoleList = userRoleService.list(wrapper);
		if (ObjectUtil.isNotEmpty(userRoleList)){
			Stream<SimpleGrantedAuthority> simpleGrantedAuthorityStream = userRoleList.stream()
				.map(o -> new SimpleGrantedAuthority(o.getRoleId()));
			List<SimpleGrantedAuthority> roleIds = simpleGrantedAuthorityStream.collect(Collectors.toList());
			return new User(userId,"",roleIds);
		}
		return null;
	}
}




